﻿package 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.BlendMode;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.geom.ColorTransform;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.AntiAliasType;
	import flash.text.engine.JustificationStyle;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	import flash.ui.Keyboard;
	import flash.utils.setInterval;
	import flash.utils.setTimeout;
	import samples.BaseSample;
	import samples.d1.*;
	import samples.d2.lines.*;
	import samples.d2.parallels.*;
	import samples.d2.parallels.trames.*;
	import samples.d2.shapes.Arc0;
	import samples.d2.trames.*;
	import shapes.shapes2D.*;
	import shapes.shapes2D.distribution.modulo.Modulo;
	import shapes.shapes2D.distribution.random.PRNG;
	import shapes.shapes2D.distribution.random.Random;
	import shapes.shapes2D.distribution.trigo.Pythagora;
	
	/**
	 * @author Nicolas Barradeau
	 * http://en.nicoptere.net
	 */
	public class Points extends Sprite 
	{
		
		private var bmp:Bitmap;
		private var bd:BitmapData;
		private var sample:BaseSample;
		
		private var sampleId:int = 0;
		private var operation:int = 0;
		private var tf:TextFormat;
		private var txt:TextField;
		
		public function Points():void 
		{
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			_stage = stage;
			
			_stage.align = StageAlign.TOP_LEFT;
			_stage.scaleMode = StageScaleMode.NO_SCALE;
			//_stage.displayState = StageDisplayState.FULL_SCREEN;
			
			_stage.addEventListener( KeyboardEvent.KEY_DOWN, keyHandler )
			
			txt = new TextField();
			txt.autoSize = TextFieldAutoSize.LEFT;
			txt.antiAliasType = AntiAliasType.ADVANCED;
			addChild( txt );
			
			tf = new TextFormat( 'Verdana', 16, 0xFFFFFF );
			sampleId = 0;
			
			keyHandler();
			
		}
		
		private function keyHandler(e:KeyboardEvent = null ):void 
		{
			if (e != null  
			&&	e.keyCode != Keyboard.RIGHT 
			&& 	e.keyCode != Keyboard.LEFT 
			&& 	e.keyCode != Keyboard.UP ) return;
			
			if (e != null )
			{
				switch( e.keyCode )
				{
					case Keyboard.RIGHT:
					sampleId++;
					break;
					
					case Keyboard.LEFT:
					sampleId--;
					break;
					
				}
			}
			//if ( sampleId > 38 ) sampleId = 38;
			if ( sampleId < 0) sampleId = 0;
			var n:int = numChildren;
			
			while ( n-- )
			{
				if( !( getChildAt( n ) is TextField ) ) removeChildAt( n );
			}
			if ( sampleId > 11 ) sampleId = 11;
			var description:String = '';
			var op:int = 0;
			switch( sampleId )
			{
				
				case op++:
					description = 'a single dot';
					sample = new Sample0();//hello world
				break;
				
				case op++:
					description = '2 dots';
					sample = new Sample1();//2 points
				break;
				
			
				
				//distribution 2d
				
				
				case op++:
					description = 'random distribution';
					sample = new RandomSample();
				break;
				
				case op++:
					description = 'pseudo random distribution';
					sample = new PRNGSample();
				break;
				
				case op++:
					description = 'circular distribution';
					sample = new CircularSample( 50 );
				break;
				
				case op++:
					description = 'distribution on a disc';
					sample = new DiscSample( 50 );
				break;
				
				case op++:
					description = 'distribution on a frame';
					sample = new FrameSample( 50 );
				break;
				
				case op++:
					description = 'distribution on a grid';
					sample = new GridSample( 50 );
				break;
				
				case op++:
					description = 'triangular distribution';
					sample = new TriangularSample( 50 );
				break;
				
				case op++:
					description = 'hexagonal distribution';
					sample = new HexagonalSample( 50 );
				break;
					
				case op++:
					description = 'pythagora in a nutshell';
					addChild( new Pythagora() );
					txt.text = sampleId.toString() +' : ' + description;
					txt.setTextFormat( tf );
					return;
				break;

				case op++:
					description = 'Poisson Disk distribution';
					sample = new PoissonSample( 50 );
				break;
				
				
			}
			
			txt.text = sampleId.toString() +' : ' + description;
			txt.setTextFormat( tf );
			if( sample != null ) addChild( sample );
			
		}
		
	}
	
}